<!DOCTYPE html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html>
<head>
<title>委外入库编辑</title>
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/logic/outsidein";
	var addressGrid;
	$(function() {
		var column1 = [{ display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '收款单编号', name:'zd001ReptCode', width: 1,hide:true},
					  { display: '物料ID', name:'zd003MaterId',  width: 1 ,hide:true},
					  { display: '<font color="gray">产品编码</font>', name:'zd004MaterCode', width: 120},
					  { display: '<font color="gray">产品名称</font>', name:'zd005MaterName', width: 120},
	                  { display: '<font color="gray">单位</font>', name: 'zd013Unit', width: 120},
	                  { display: '<font color="gray">规格</font>', name: 'zd007MaterSpec', width: 100},
	                  { display: '产品尺寸', name: 'zd010MaterSize', width: 80,editor: { type: 'text'}},
	   				  { display: '产品颜色', name: 'zd011MaterColor', width: 80,editor: { type: 'text'}},
	   				  { display: '产品材质', name: 'zd012MaterQuanlity', width: 160,editor: { type: 'text'}},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'zd014TaxPrice', width: 100}, 
	                  { display: '数量<font color="red">*</font>', name: 'zd018InQuantity', width: 100,editor: { type: 'float'},render:function(item){
	                	  if(item.zd018InQuantity == null || item.zd018InQuantity == "" || parseFloat(item.zd018InQuantity) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd018InQuantity*1).toFixed(4);
	                	  }
	                  }}
		              ];
		var column2 = [{ display: 'id', name:'id', width: 1 ,hide:true},
						  { display: '收款单编号', name:'zd001ReptCode', width: 1,hide:true},
						  { display: '物料ID', name:'zd003MaterId',  width: 1 ,hide:true},
						  { display: '<font color="gray">产品编码</font>', name:'zd004MaterCode', width: 120},
						  { display: '<font color="gray">产品名称</font>', name:'zd005MaterName', width: 120},
		                  { display: '<font color="gray">单位</font>', name: 'zd013Unit', width: 120},
		                  { display: '<font color="gray">规格</font>', name: 'zd007MaterSpec', width: 100},
		                  { display: '产品尺寸', name: 'zd010MaterSize', width: 80},
		   				  { display: '产品颜色', name: 'zd011MaterColor', width: 80 },
		   				  { display: '产品材质', name: 'zd012MaterQuanlity', width: 160},
		                  { display: '<font color="gray">单价(含税)</font>', name: 'zd014TaxPrice'}, 
		                  { display: '<font color="gray">数量</font>', name: 'zd018InQuantity', width: 100}
			              ];
		var column3 = null;
		column3 = column1;
		addressGrid = $("#dataGrid1").ligerGrid({
			columns: column3,
            enabledEdit: true,
            usePager: true,pageSize:1000,pageSizeOptions:[1000],
            parms:[{ name : 'outSideInId' , value : '${tsk.id}'}],
            url : basic_Url+"/gridItems.do",
            width: "100%", height:'90%', checkbox: true,
            toolbar: { items: [{ text: '添加物料', click: ItemWindow, icon: 'add'},
                               { line: true },
                               { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                       ]}
            });
		//仓库
		$("#zd003WhId").change(function(){
			//仓库编码
			var whCode = $("#zd003WhId").find("option:selected").attr("cvalue");
			$("#zd003WhCode").val(whCode);
			//仓库名称
			var whName = $("#zd003WhId").find("option:selected").text();
			$("#zd003WhName").val(whName);
		});
		//按钮
		if('ADD' == '${flag}'){
			$("#forSubmit").hide();
			$("#forPass").hide();
			$("#forBack").hide();
		}else{
			var billState = '${tsk.zd001Status}';
			if('OUTSIDEIN_SAVE' == billState){
				$("#forPass").hide();
				$("#forBack").hide();
			}
			if('OUTSIDEIN_SUBMIT' == billState){
				$("#forSave").hide();
				$("#forSubmit").hide();
			}
			if('OUTSIDEIN_END' == billState){
				$("#forSave").hide();
				$("#forSubmit").hide();
				$("#forPass").hide();
				$("#forBack").hide();
			}
			if('OUTSIDEIN_BACK' == billState){
				$("#forSave").hide();
				$("#forPass").hide();
				$("#forBack").hide();
			}
		}
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler : function(editform) {

				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;

					//getReturnMessage(url, "inputForm", success_function);
					getReturnMessage(url, "inputForm", W.searchAP);
				} catch (e) {
					alert(e);
				}
			}
		});
	});
	function submitForm(param) {
	 	$("#addressInfos").val(JSON.stringify($("#dataGrid1").ligerGetGridManager().getData()));
		var data = $("#dataGrid1").ligerGetGridManager().getData();
		var datadele = $("#dataGrid1").ligerGetGridManager().getDeleted();
		if(data.length==0){
			$.dialog.alert("请增加物料信息");
	   	 	return false;
		}
		var ba=true;
		if(data !=null){
			for (var i = 0;i < data.length;i++){
				var row = data[i];
				if(undefined == row.zd018InQuantity|| isNaN(row.zd018InQuantity)|| row.zd018InQuantity <= 0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“入库数量”不合法");
		           	 ba=false;
		           	 return false;
		        }
			}
		}
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if(ba){
			$("#submitFlag").val(param);
			$("#dataitem").val(JSON.stringify(data));
	        $("#datadele").val(JSON.stringify(datadele));
			$("#inputForm").submit();
		}
	}
	function check(){
		$.ajax({
		      url: "${ctx}/logic/outsidein/updateState.do",
		      global: false,
		      type: "POST",
		      dataType: "json",
		      data: ({
		    	  outSideInId : $("#id").val()
		      }),		     
		      success: function(msg){
		    	  if(msg.msg=="SUCCESS"){
		    		  $.dialog.alert("驳回成功",function(){close();});
		    	  }
		    	  if(msg.msg=="ERROR"){
		    		  $.dialog.alert("操作失败",function(){close();});
		    	  }
		   }
		});
	}
	//关闭消息提示框
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
	//增加物料
	function ItemWindow(){
		getDG().dialog({
			title : '选择材料',
			width : 650,
			height : 350,
			content : 'url:' + sys_context+ '/public/windowalert/itemmanager.do',
			lock : true,
			max : true,
			min : true,
			ok : function() {
				var getSelectedHub = this.content.getSelected();
				var manager = $("#dataGrid1").ligerGetGridManager();
				if (getSelectedHub != null) {
					$(getSelectedHub).each(function(){
		                manager.addRow2({
		                	zd003MaterId : this.id,
		                	zd004MaterCode : this.zd001Code,
		                	zd005MaterName : this.zd002Name,
		                	zd007MaterSpec : this.zd006Description,
		                	zd008MaterModel : this.zd004Model,
		                	zd010MaterSize : this.zd026Size,
		                	zd011MaterColor : this.zd033Color,
		                	zd012MaterQuanlity : this.zd034Quality,
		                	zd013Unit : this.basicStapleUnitDO.zd002Name,
		                	zd014TaxPrice : this.zd009Price
	                	});
					});
					this.content.Close();
				}
				return false;
			},
			cancelVal : '关闭',
			cancel : true
		}).zindex();
	}
	function deleteRow(){
		addressGrid.deleteSelectedRow();
	}
	//如果是委外出库后的入库，要选择委外出库单号
	function outSideOutReturnWindow(){
		getDG().dialog({
			title : '选择委外出库单',
			width : 650,
			height : 350,
			content : 'url:' + sys_context + '/public/windowalert/outSideOut.do',    
			lock : true,
			max : true,
			min : true,
			ok : function() {
				var rowSel = this.content.getSelected();
				if(rowSel==null){
					alert('请选择委外出库单!');
					return;
				}else{
					if(rowSel.length>1){
						alert('只能选择一张委外出库单!');
						return;
					}else{
						$(rowSel).each(function() {
							$("#zd002OriginId").val(this.id);
							$("#zd002OriginCode").val(this.zd001Code);
							//带出出库单的物料
							multipleSearch("#dataGrid1",true,"",basic_Url+"/outSideOutItem.do?outSideOutId="+this.id);
						});
					}
				}
				this.content.Close();
				return false;
			},
			cancelVal : '关闭',
			cancel : true
		}).zindex();
	}
</script>
</head>
<body>
	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${tsk.id}" />
		<input type="hidden" id="dataitem" name="dataitem"/>
		<input type="hidden" id="datadele" name="datadele"/>
		<input type="hidden" id="bizVersion" name="bizVersion" value="${tsk.bizVersion}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" name="submitFlag" value="" />
		<input type="hidden" id="zdType" name="zdType" value="${zdType }">
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock">
					<c:if test="${'exter' == zdType }">
						<c:if test="${not empty tsk.zd001Code}">
							<td nowrap>单号:</td>
							<td>
								<input class="inp_width_98p" type="text" id="zd001Code" name="zd001Code" readonly="readonly"  value="${tsk.zd001Code}" />
							</td>
							<td colspan="2"></td>
						</c:if>
					</c:if>
					<c:if test="${'inter' == zdType }">
						<tr>
							<td nowrap>委外出库单号:</td>
							<td>
								<input type="hidden" id="zd002OriginId" name="zd002OriginId" />
	   		                 	<input type="text" style="width:140px;" id="zd002OriginCode"  name="zd002OriginCode" class="inp_width_98p required"/>
   		        				<input type="button" value="..." onclick="outSideOutReturnWindow();" />
							</td>
							<td colspan="2"></td>
						</tr>
					</c:if>
					<tr>
					 	<td nowrap>入库仓库:</td>
						<td>
							<select id="zd003WhId" name="zd003WhId" class="inp_width_98p required" >
								<option></option>
								<c:forEach items='${sgfList}' var='sgf'>
	   		                 		<option value='${sgf.id}' cvalue="${sgf.zd002Code }" <c:if test="${tsk.zd003WhId eq sgf.id }">selected</c:if>>${sgf.zd003Name}</option>
	   		                 	</c:forEach>
							</select>
							<input type="hidden" id="zd003WhCode" name="zd003WhCode" value="${tsk.zd003WhCode }"/>
   		             		<input type="hidden" id="zd003WhName" name="zd003WhName" value="${tsk.zd003WhName }"/>
						</td>
					 	<td nowrap>入库时间:</td>
						<td>
							<input id="zd006Date" name="zd006Date" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${tsk.zd006Date}" pattern="yyyy-MM-dd" type="both" />" />
						</td>
					 	<c:if test="${tsk.zd006Date == null}">
							<script>
								var date = new Date();
								$("#zd006Date").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
							</script>
						</c:if>
					</tr>
					<tr>
						<td nowrap>PI单号:</td>
						<td> 
							<input type="text" id="zd009PiCode" name="zd009PiCode" class="inp_width_98p" value="${tsk.zd009PiCode}" />
						</td>
						<td nowrap>制单人:</td>
						<td> 
							<input type="text" id="zd005Aegnt" name="zd005Aegnt" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
							<c:if test="${tsk.zd005Aegnt != null}">
								<script>
									$("#zd005Aegnt").val("${tsk.zd005Aegnt}");
								</script>
							</c:if>
						</td>
					</tr>
					<tr>
						<td nowrap>备注:</td>
						<td colspan="3"><input class="inp_width_98p"  type="text" id="zd010Remark" name="zd010Remark"  value="${tsk.zd010Remark}" /></td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;">
							<div id="dataGrid1"></div>
						</td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSave" onclick="submitForm('OUTSIDEIN_SAVE')">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSubmit" onclick="submitForm('OUTSIDEIN_SUBMIT')">提交</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_OIWH_RK_SP" description="委外入库单审批">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPass" onclick="submitForm('OUTSIDEIN_END')">审核通过</button></div></div></li>
			    </webres:displaycontroller>
			    <webres:displaycontroller elementId="LOGIC_OIWH_RK_PH" description="委外入库单驳回">   
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forBack" onclick="check()">驳回</button></div></div></li>
		       </webres:displaycontroller>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
	</form>
</body>
</html>
